#!/usr/bin/env python
# -*- coding: utf_8 -*-
# @Author: IcySun
#脚本功能：查找IP的归属地。调用新浪接口
import sys
reload(sys)
sys.setdefaultencoding("utf-8")

import urllib2,re

def getaddress(ip):
    url = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=' + ip
    redict = re.compile(r'var remote_ip_info = (.*?);')
    try:
        con = urllib2.urlopen(url).read()
        address = eval(re.findall(redict, con)[0])
        country = address['country'].decode('unicode_escape')
        province = address['province'].decode('unicode_escape')
        city = address['city'].decode('unicode_escape')
        message = '%-15s --->  %s,%s,%s' % (ip,country,province,city)
        print message
    except Exception, e:
        pass

def main():
    for ip in open('ip1.txt','r').readlines():
        getaddress(ip.strip('\n'))

if __name__ == '__main__':
    main()